package com.example.myapplication2.app;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BroadcastActivity extends Activity {
    private Receiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_boardcast);
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        TextView networkState = (TextView) findViewById(R.id.network_state_content);
        if (networkInfo != null && networkInfo.isAvailable()) {
            networkState.setText("可用");
        } else {
            networkState.setText("不可用");

        }
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        TextView wifiText = (TextView) findViewById(R.id.wifi_title);

        if (wifiManager.isWifiEnabled()) {
            LinearLayout wifi_info = (LinearLayout) findViewById(R.id.wifi_info);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            wifi_info.setVisibility(View.VISIBLE);
            TextView wifi_info_text = (TextView) findViewById(R.id.wifi_info_text);
            StringBuilder sb = new StringBuilder();
            sb.append("MAC Address:").append(wifiInfo.getMacAddress()).append("\n");
            sb.append("IP Address:").append(wifiInfo.getIpAddress()).append("\n");
            sb.append("SSID:").append(wifiInfo.getSSID()).append("\n");
            sb.append("连接速度:").append(wifiInfo.getLinkSpeed()).append("\n");
            wifi_info_text.setText(sb);
            wifiText.setText("开启中");
        } else {
            wifiText.setText("关闭中");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        receiver = new Receiver();
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    class Receiver extends BroadcastReceiver {
        int intLevel = 0;
        int intScale = 0;

        @Override
        public void onReceive(Context context, Intent intent) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.battery);
            layout.setVisibility(View.VISIBLE);
            TextView battery_text = (TextView) findViewById(R.id.battery_text);
            String BatteryStatus = null;
            /*
             * 如果捕捉到的action是ACTION_BATTERY_CHANGED， 就运行onBatteryInfoReceiver()
             */
            intLevel = intent.getIntExtra("level", 0);
            intScale = intent.getIntExtra("scale", 100);


            switch (intent.getIntExtra("status",
                    BatteryManager.BATTERY_STATUS_UNKNOWN)) {
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    BatteryStatus = "充电状态";
                    break;
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    BatteryStatus = "放电状态";
                    break;
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    BatteryStatus = "未充电";
                    break;
                case BatteryManager.BATTERY_STATUS_FULL:
                    BatteryStatus = "充满电";
                    break;
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    BatteryStatus = "未知道状态";
                    break;
            }
//                switch (intent.getIntExtra("plugged",
//                        BatteryManager.BATTERY_PLUGGED_AC)) {
//                    case BatteryManager.BATTERY_PLUGGED_AC:
//                        BatteryStatus2 = "AC充电";
//                        break;
//                    case BatteryManager.BATTERY_PLUGGED_USB:
//                        BatteryStatus2 = "USB充电";
//                        break;
//                }

//                switch (intent.getIntExtra("health",
//                        BatteryManager.BATTERY_HEALTH_UNKNOWN)) {
//                    case BatteryManager.BATTERY_HEALTH_UNKNOWN:
//                        BatteryTemp = "未知错误";
//                        break;
//                    case BatteryManager.BATTERY_HEALTH_GOOD:
//                        BatteryTemp = "状态良好";
//                        break;
//                    case BatteryManager.BATTERY_HEALTH_DEAD:
//                        BatteryTemp = "电池没有电";
//                        break;
//                    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
//                        BatteryTemp = "电池电压过高";
//                        break;
//                    case BatteryManager.BATTERY_HEALTH_OVERHEAT:
//                        BatteryTemp = "电池过热";
//                        break;
//                }
            StringBuilder sb = new StringBuilder();
            sb.append("电池状态:" + BatteryStatus + "\n");
            sb.append("电池电量:" + intScale + "\n");
            battery_text.setText(sb.toString());
        }
    }

}
